package edu.gettysburg.catapp1;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.os.Bundle;
import android.os.Environment;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

/**
 * Tour Intro displays the initial introductions for the tour: title, image, text description
 * String imgpath: placeholder for the path to fetch image
 * String textpath: placeholder for the path to fetch text
 */
public class TourOverview extends Activity {
	private String imgpath;
	private String textpath;

	public static String TOUR_NAME = "";
	public static Tour TOUR_OBJECT;

	@TargetApi(16)
	/*
	 * Dynamically creates tour introduction based on:
	 * String message: title obtained from ChooseTour.java
	 * String imgpath: location of tour image in file directory
	 * String textpath: location of text chunk in file directory
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		TOUR_NAME = intent.getStringExtra(getPackageName());
		setContentView(R.layout.activity_tour_overview);
		getActionBar().hide();


		Button b1 = (Button) findViewById(R.id.btn1);

		b1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), InitialSite.class);
				startActivityForResult(intent, 1);
			}
		});

		TextView tv = (TextView) findViewById(R.id.title);
		tv.setText(TOUR_NAME);

		File[] tourlist = getBaseContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).listFiles();
		String path = "";
		for(int i = 0; i < tourlist.length; i++) {
			if(tourlist[i].getName().equals(TOUR_NAME)) {
				path = tourlist[i].getPath();
				break;
			}
		}

		final String tourpath = path;
		
		TOUR_OBJECT = decode(tourpath);

		ImageView iv = (ImageView) findViewById(R.id.img);
		iv.setBackground(null);

		TextView text = (TextView) findViewById(R.id.descrip);
		text.setText(TOUR_OBJECT.getDescription());

		//TODO: retrieve image and text correctly to display appropriate files
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * Creates standard action bar menu options
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_tour_overview, menu);
		return true;
	}


	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * Sets listeners for action bar items; however, overridden by hidden action bar
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
			//TODO: finish action bar for intro. include continue and directions
		}
		return super.onOptionsItemSelected(item);
	}

	private Tour decode(String path) {
		try {
			FileInputStream fis = new FileInputStream(path + "/tour.dat");
			ObjectInputStream ois = new ObjectInputStream(fis);
			Tour tour = (Tour) ois.readObject();
			ois.close();
			Log.v("tour.dat", "read: ok");
			return tour;
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}